﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;

namespace OA.Ajax
{
    /// <summary>
    /// SendEmail 的摘要说明
    /// </summary>
    public class SendEmail : IHttpHandler
    { 

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string sender = context.Request["senderp"];
            string pwd = context.Request["pwd"];
            string shoujianren = context.Request["shoujianren"];
            string zhuti = context.Request["zhuti"];
            string content = context.Request["content"];

            string[] shoujian = shoujianren.Split('\n');
            for (int i = 0; i < shoujian.Length-1; i++)
            {
                try
                {
                    SendMail(sender, pwd, shoujian[i], zhuti, content, "SMTP.163.com");
                    context.Response.Write("用户：" + sender + ",邮箱：" + shoujian[i] + ",状态：发送成功！ \n");
                }
                catch(Exception ex)
                {
                    context.Response.Write("用户：" + sender + ",邮箱：" + shoujian[i] + ",状态：发送失败！原因：'"+ex.ToString()+"' \n");
                }
                
            }

        }
        public void SendMail(string Email, string pwd, string ToEmail, string Subject, string body, string Smp_Type)
        {
            MailMessage myMail = new MailMessage();
            myMail.From = new MailAddress(Email);
            myMail.To.Add(ToEmail);
            myMail.Subject = Subject;
            myMail.Body = body;
            myMail.SubjectEncoding = System.Text.UnicodeEncoding.UTF8;
            myMail.BodyEncoding = System.Text.UnicodeEncoding.UTF8;
            myMail.IsBodyHtml = true;
            myMail.Priority = MailPriority.High;
            System.Net.Mail.SmtpClient myclient = new SmtpClient();
            myclient.Host = Smp_Type;
            myclient.Port = 25;
            myclient.Credentials = new NetworkCredential(Email, pwd);
            myclient.Send(myMail);


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}